﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            //检查输入
            bool b = 检查数字(textBox1.Text, textBox2.Text, comboBox1.Text);
            if (false == b)
            {
                return;
            }
            textBox3.Text = 计算结果(textBox1.Text, textBox2.Text, comboBox1.Text).ToString();

        }

        public double 计算结果(string N1, string N2, string YSF)
        {
            bool b = false;
            double n1 = 获取数字(N1, ref b);
            double n2 = 获取数字(N2, ref b);
            switch (YSF)
            {
                case "+":
                    return n1 + n2;
                case "-":
                    return n1 - n2;
                case "*":
                    return n1 * n2;
                case "/":
                    return n1 / n2;
            }
            return 0;
        }



        /// <summary>
        /// 检查输入的数据
        /// </summary>
        /// <param name="N1">第一数字</param>
        /// <param name="N2">第二数字</param>
        /// <param name="YSF">运算符</param>
        /// <returns>返回是不是数字，是：true</returns>
        public bool 检查数字(string N1, string N2, string YSF)
        {
            if ("" == YSF)
            {
                MessageBox.Show("请选择运算符");
                return false;
            }
            bool b = false;
            获取数字(N1, ref b);
            if (false == b)
            {
                MessageBox.Show("第一个参数不是合法的数字");
                return false;
            }
            获取数字(N2, ref b);
            if (false == b)
            {
                MessageBox.Show("第二个参数不是合法的数字");
                return false;
            }
            if ("/" == YSF && "0" == N2)
            {
                MessageBox.Show("除法运算时，第二个参数不能为0");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 检查输入的数字是否合法
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="b"></param>
        /// <returns></returns>

        public double 获取数字(string str, ref bool b)
        {
            double number = 0;
            if (double.TryParse(str, out number))
            {
                b = true;
                return number;
            }
            else
            {
                b = false;
                return 0;
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            ysf = "";
            textBox4.Text = "";
            n1 = "";

        }

        public string n1,n2, ysf;

        private void button3_Click(object sender, EventArgs e)
        {
            DealNumber("7");
        }

        private void button4_Click(object sender, EventArgs e)
        {

            DealNumber("8");
        }

        private void button5_Click(object sender, EventArgs e)
        {

            DealNumber("9");
        }

        private void button8_Click(object sender, EventArgs e)
        {

            DealNumber("4");
        }

        private void button7_Click(object sender, EventArgs e)
        {

            DealNumber("5");
        }

        private void button6_Click(object sender, EventArgs e)
        {

            DealNumber("6");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            DealNumber("1");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            DealNumber("2");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            DealNumber("3");
        }

        private void button13_Click(object sender, EventArgs e)
        {
            DealNumber("0");
        }

        private void button14_Click(object sender, EventArgs e)
        {
            DealNumber("+/-");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            DealNumber(".");
        }

        private void button18_Click(object sender, EventArgs e)
        {
            DealYsf("+");
        }
        public  void DealYsf(string arg)
        {
            if (""==textBox4.Text)
            {
                return;
            }
            else
            {
                n1 = textBox4.Text;
                ysf = arg;
                n2cls = false;
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            DealYsf("-");
        }

        private void button16_Click(object sender, EventArgs e)
        {
            DealYsf("*");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            DealYsf("/");
        }


       public bool n2cls = false;
        public void DealNumber(string arg)
        {
            if (""==ysf)
            {
                DealTxt(arg);
            }
            else
            {
                if (false == n2cls)
                {
                    textBox4.Text = "";
                    n2cls = true;
                }
                DealTxt(arg);
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double num = 0;
            double.TryParse(textBox4.Text,out num);
            textBox4.Text = (num * num).ToString();
            n2cls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double num = 0;
            double.TryParse(textBox4.Text, out num);
            textBox4.Text = Math.Sqrt( num).ToString();
            n2cls = false;
        } 

        public void DealTxt(string arg)
        {
            if ("+/-" == arg)
            {
                bool b = false;
                double n1 = 获取数字(textBox4.Text, ref b);
                textBox4.Text = (-1 * n1).ToString();
            }
            else if ("." == arg)
            {
                if (textBox4.Text.Contains("."))
                {
                    return;
                }
                else
                {
                    textBox4.Text = textBox4.Text + ".";
                }
            }
            else
            {
                textBox4.Text = textBox4.Text + arg;
            }
        }


        private void button19_Click(object sender, EventArgs e)
        {

            bool b = false;
            //检查数据
            b = 检查数字(n1, textBox4.Text, ysf);
            if (false == b)
            {
                return;
            }

            textBox5.AppendText(Environment.NewLine+n1+ysf+textBox4.Text);
            textBox4.Text = 计算结果(n1, textBox4.Text, ysf).ToString();
            textBox5.Text = textBox5.Text +"="+ textBox4.Text;
            n2cls = false;
        }
    }
}
